<template>
    <div class="uc-main">
      <div v-if="userInfo">
        <h2>个人资料</h2>
        <div class="profile-info">
          <div class="info-field" v-for="(info, index) in editableFields" :key="info.key">
            <label :for="info.key">{{ info.label }}:</label>
            <input
              v-if="info.editable"
              type="text"
              :id="info.key"
              v-model="userInfo[info.key]"
              :placeholder="info.placeholder"
            />
            <span v-else>{{ userInfo[info.key] }}</span>
          </div>
          <button @click="updateUserInfo">提交修改</button>
        </div>
      </div>
  
      <div v-if="passwords">
        <h2>密码安全</h2>
        <div class="password-info">
          <div class="info-field">
            <label for="oldPassword">原始密码:</label>
            <input type="password" id="oldPassword" v-model="passwords.oldPassword" />
          </div>
          <div class="info-field">
            <label for="newPassword">新密码:</label>
            <input type="password" id="newPassword" v-model="passwords.newPassword" />
          </div>
          <div class="info-field">
            <label for="confirmPassword">确认密码:</label>
            <input type="password" id="confirmPassword" v-model="passwords.confirmPassword" />
          </div>
          <button @click="updatePassword">确认修改</button>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { defineProps, ref } from 'vue';
  
  const props = defineProps({
    userInfo: {
      type: Object,
      required: true,
    },
    passwords: {
      type: Object,
      required: false,
    },
  });
  
  // 用户信息表单字段
  const editableFields = [
    { label: '用户手机号', key: 'userphone', editable: true, placeholder: '请输入用户手机号' },
    { label: '邮箱', key: 'email', editable: true, placeholder: '请输入邮箱' },
    { label: '地址', key: 'useraddress', editable: true, placeholder: '请输入地址' },
  ];
  
  // 更新用户信息函数（这里需要实现实际的API调用逻辑）
  const updateUserInfo = () => {
    console.log('提交的用户信息:', props.userInfo);
  };
  
  // 更新密码函数（这里需要实现实际的API调用逻辑）
  const updatePassword = () => {
    if (props.passwords.newPassword !== props.passwords.confirmPassword) {
      alert("新密码和确认密码不匹配！");
      return;
    }
    console.log('提交的密码信息:', props.passwords);
  };
  </script>
  
  <style scoped>
  .uc-main {
    flex: 3;
    padding: 20px;
  }
  
  .profile-info, .password-info {
    margin-bottom: 20px;
  }
  
  .info-field {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
  
  label {
    margin-right: 10px;
  }
  
  input[type="text"],
  input[type="password"] {
    flex: 1;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  
  button {
    padding: 10px 15px;
    background-color: #409eff;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
  }
  
  button:hover {
    background-color: #66b1ff;
  }
  </style>
  